package com.jbm.jbmsupplier.jbmpay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.onehome.iface.IAplipay;

import java.util.Map;

/**
 * Created by JBMhaier on 2017/4/27.
 */

public class ALpayUtil {
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    private Context context;
    private  Activity aty;
    private  SuccessListener listener;
    private final int ALCHONGERROR=0301;
    public ALpayUtil(Context context, Activity aty,SuccessListener listener){
        this.context=context;
        this.aty=aty;
        this.listener=listener;
       net = new NetIntent(mHandler);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                     //   Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
                        listener.sucess("支付成功");
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                     //   Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                        listener.fail("支付失败");
                    }
                    break;
                }
//                case SDK_AUTH_FLAG: {
//                    @SuppressWarnings("unchecked")
//                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
//                    String resultStatus = authResult.getResultStatus();
//
//                    // 判断resultStatus 为“9000”且result_code
//                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
//                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
//                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
//                        // 传入，则支付账户为该授权账户
//                        Toast.makeText(context,
//                                "授权成功\n" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT)
//                                .show();
//                    } else {
//                        // 其他状态值则为授权失败
//                        Toast.makeText(context,
//                                "授权失败" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT).show();
//
//                    }
//                    break;
//                }
                case ALCHONGERROR:
                    IAplipay pay = (IAplipay) msg.obj;
                    if (pay.m_info.equals("OK")) {
                        final String orderInfo = pay.returnString;//pay.returnString
                        Runnable payRunnable = new Runnable() {

                            @Override
                            public void run() {
                                PayTask alipay = new PayTask(aty);
                                Map<String, String> result = alipay.payV2(orderInfo, true);
                                Log.i("msp", result.toString());
                                Message msg = new Message();
                                msg.what = SDK_PAY_FLAG;
                                msg.obj = result;
                                mHandler.sendMessage(msg);
                            }
                        };

                        Thread payThread = new Thread(payRunnable);
                        payThread.start();
                    }else{

                        listener.getError(pay.m_info);
                    }

                    break;

                default:
                    break;
            }
        };
    };
    NetIntent net;

    /**
     * 订单用的  这里还需要改
     */
//    public void setPay(){

//        net.getPayDemo("951357",0.01);
//    }

    /**
     * 充值
     * @param amout
     * @param payname
     */
    public void chong(double amout,String payname){
        net.getAplipayRehargeC(ALCHONGERROR,amout,payname);
    }




    public interface SuccessListener{
        void sucess(String str);
        void fail(String str);
        void getError(String str);
    }


}
